package com.gzhryc.shared_device.oem.code.place.services;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.place.dao.PlacePhysicalStoreUserDao;
import com.gzhryc.shared_device.oem.code.place.dao.models.PlacePhysicalStoreUser;

import java.sql.SQLException;
import java.util.List;

public class PlacePhysicalStoreUserService {

    static Logger log = Logger.getLogger(PlacePhysicalStoreUserService.class);

    public static PlacePhysicalStoreUserService self() {
        return MultiDBTools.getService(PlacePhysicalStoreUserService.class, true);
    }

    public static PlacePhysicalStoreUserService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, PlacePhysicalStoreUserService.class, true);
    }

    PlacePhysicalStoreUserDao dao;

    public PlacePhysicalStoreUserService(String jdbcKey) {
        this.dao = new PlacePhysicalStoreUserDao(jdbcKey);
    }

    public List<PlacePhysicalStoreUser> findByUserId(Long userId, Integer pageNo, Integer pageLimit){
        if(userId != null && pageNo != null && pageLimit != null){
            Conditions conditions = new Conditions(PlacePhysicalStoreUser.class);
            conditions.and().is("userId",userId);

            try {
                return dao.findJoinByCondition(conditions,pageNo,pageLimit);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public Long countByUserId(Long userId){
        if(userId != null){
            Conditions conditions = new Conditions(PlacePhysicalStoreUser.class);
            conditions.and().is("userId",userId);

            try {
                return dao.countJoin(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return 0L;
    }
}
